<template>
	<view class="page">
		<view class="info_box" style="padding-bottom: 40rpx;">
			<view class="info_box_base">
				<view class="info_box_base_title" style="font-weight: bold;">室外接入信号测试</view>
				<view class="info_box_base_title" style="font-weight: bold;margin-top: 20rpx;">5G测试数据</view>
				<view class="info_box_base_item">
					<text>5G频段：</text>
					<u--input placeholder="请输入5G频段" v-model="params.frequency_5Gband" border="bottom"></u--input>
				</view>

				<view class="info_box_base_item">
					<view class="info_box_base_item" style="width: 50%;">
						<text>NR-CI：</text>
						<u--input placeholder="请输入NR-CI" v-model="params.nr_cl" border="bottom"></u--input>
					</view>
					<view class="info_box_base_item" style="width: 50%;">
						<text>NR-ARFCN：</text>
						<u--input placeholder="请输入NR-ARFCN" v-model="params.nr_arfcn" border="bottom"></u--input>
					</view>
				</view>

				<view class="info_box_base_item">
					<view class="info_box_base_item" style="width: 50%;">
						<text>SS-RSRP：</text>
						<u--input placeholder="请输入SS-RSRP" v-model="params.ss_rsrp" border="bottom"></u--input>
					</view>
					<view class="info_box_base_item" style="width: 50%;">
						<text>SS-RINR：</text>
						<u--input placeholder="请输入SS-RINR" v-model="params.ss_rlnr" border="bottom"></u--input>
					</view>
				</view>

				<view class="info_box_base_item">
					<view class="info_box_base_item" style="width: 50%;">
						<text>下载速率：</text>
						<u--input placeholder="请输入下载速率" v-model="params.download_5grate" border="bottom"></u--input>
					</view>
					<view class="info_box_base_item" style="width: 50%;">
						<text>上传速率：</text>
						<u--input placeholder="请输入上传速率" v-model="params.upload_5gspeed" border="bottom"></u--input>
					</view>
				</view>

				<view class="info_box_base_title" style="font-weight: bold;margin-top: 20rpx;">4G测试数据</view>
				<view class="info_box_base_item">
					<text>4G频段：</text>
					<u--input placeholder="请输入4G频段" v-model="params.frequency_4Gband" border="bottom"></u--input>
				</view>
				<view class="info_box_base_item">
					<view class="info_box_base_item" style="width: 50%;">
						<text>ECI：</text>
						<u--input placeholder="请输入ECI" v-model="params.ecl" border="bottom"></u--input>
					</view>
					<view class="info_box_base_item" style="width: 50%;">
						<text>EARFCN：</text>
						<u--input placeholder="请输入EARFCN" v-model="params.earfcn" border="bottom"></u--input>
					</view>
				</view>
				<view class="info_box_base_item">
					<view class="info_box_base_item" style="width: 50%;">
						<text>RSRP：</text>
						<u--input placeholder="请输入RSRP" v-model="params.rsrp" border="bottom"></u--input>
					</view>
					<view class="info_box_base_item" style="width: 50%;">
						<text>SINR：</text>
						<u--input placeholder="请输入SINR" v-model="params.slnr" border="bottom"></u--input>
					</view>
				</view>
				<view class="info_box_base_item">
					<view class="info_box_base_item" style="width: 50%;">
						<text>下载速率：</text>
						<u--input placeholder="请输入下载速率" v-model="params.download_4grate" border="bottom"></u--input>
					</view>
					<view class="info_box_base_item" style="width: 50%;">
						<text>上传速率：</text>
						<u--input placeholder="请输入上传速率" v-model="params.upload_4gspeed" border="bottom"></u--input>
					</view>
				</view>

				<view class="info_box_base_item_1">
					<view class="info_box_base_item_img_title">5G测试照片</view>
					<view class="info_box_base_item_img_box">
						<u-upload :fileList="fileList1" width="65" height="65" @afterRead="afterRead"
							@delete="deletePic" name="1" multiple :maxCount="10"></u-upload>
					</view>
				</view>

				<view class="info_box_base_item_1">
					<view class="info_box_base_item_img_title">4G测试照片</view>
					<view class="info_box_base_item_img_box">
						<u-upload :fileList="fileList2" width="65" height="65" @afterRead="afterRead"
							@delete="deletePic" name="2" multiple :maxCount="10"></u-upload>
					</view>
				</view>

			</view>

			<view class="info_box_base">
				<view class="info_box_base_title" style="font-weight: bold;">覆盖后室内信号测试</view>

				<view class="info_box_base_title" style="font-weight: bold;margin: 20rpx 0;">覆盖设备</view>
				<view class="info_box_base_btns">
					<view v-for="(item,index) in coverageofdevice_testingdata">
						<!-- <view @click="chooseSb(item,index)" class="info_box_base_btns_item info_box_base_btns_item_active" v-if="isInArray(item.id, currentSbids)">{{item.number_code}}号设备</view> -->
						<view @click="chooseSb(item, index)" class="info_box_base_btns_item"
							:class="item.is_full?'':'info_box_base_btns_item_active'">{{item.number_code}}号设备
						</view>
					</view>

				</view>
				<view>
					<view class="info_box_base_title" style="font-weight: bold;margin-top: 20rpx;">5G测试数据</view>
					<view class="info_box_base_item">
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>SS-RSRP：</text>
							<u--input v-model="current_coverageofdevice_testingdata.coveragedevice_5gss_rsrp"
								placeholder="请输入SS-RSRP" border="bottom"></u--input>
						</view>
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>SS-RINR：</text>
							<u--input placeholder="请输入SS-RINR"
								v-model="current_coverageofdevice_testingdata.coveragedevice_5gss_rinr"
								border="bottom"></u--input>
						</view>
					</view>
					<view class="info_box_base_item">
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>下载速率：</text>
							<u--input placeholder="请输入下载速率"
								v-model="current_coverageofdevice_testingdata.coveragedevice_5gdownloadrate"
								border="bottom"></u--input>
						</view>
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>上传速率：</text>
							<u--input placeholder="请输入上传速率"
								v-model="current_coverageofdevice_testingdata.coveragedevice_5guploadspeed"
								border="bottom"></u--input>
						</view>
					</view>

					<view class="info_box_base_title" style="font-weight: bold;margin-top: 20rpx;">4G测试数据</view>
					<view class="info_box_base_item">
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>RSRP：</text>
							<u--input placeholder="请输入RSRP"
								v-model="current_coverageofdevice_testingdata.coveragedevice_4grsrp"
								border="bottom"></u--input>
						</view>
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>SINR：</text>
							<u--input placeholder="请输入SINR"
								v-model="current_coverageofdevice_testingdata.coveragedevice_4gsinr"
								border="bottom"></u--input>
						</view>
					</view>
					<view class="info_box_base_item">
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>下载速率：</text>
							<u--input placeholder="请输入下载速率"
								v-model="current_coverageofdevice_testingdata.coveragedevice_4gdownloadrate"
								border="bottom"></u--input>
						</view>
						<view class="info_box_base_item" style="width: 50%;">
							<text><span style="color: red;">*</span>上传速率：</text>
							<u--input placeholder="请输入上传速率"
								v-model="current_coverageofdevice_testingdata.coveragedevice_4guploadspeed"
								border="bottom"></u--input>
						</view>
					</view>
					<view class="info_box_base_item_1">
						<view class="info_box_base_item_img_title"><span style="color: red;">*</span>5G测试照片</view>
						<view class="info_box_base_item_img_box">
							<u-upload :fileList="fileList3" width="65" height="65" @afterRead="afterRead"
								@delete="deletePic" name="3" multiple :maxCount="10"></u-upload>
						</view>
					</view>
					<view class="info_box_base_item_1">
						<view class="info_box_base_item_img_title"><span style="color: red;">*</span>4G测试照片</view>
						<view class="info_box_base_item_img_box">
							<u-upload :fileList="fileList4" width="65" height="65" @afterRead="afterRead"
								@delete="deletePic" name="4" multiple :maxCount="10"></u-upload>
						</view>
					</view>
				</view>

			</view>


			<view class="btn" @click="next">下一步</view>
		</view>
	</view>
</template>

<script>
	import { forEach } from '../../js_sdk/luch-request/luch-request/utils';
import {
		api,
		apiBaseUrl,
		http
	} from '@/config/common.js'
	export default {
		data() {
			return {
				id: '',
				currentSbids: [],
				params: {
					frequency_5Gband: '',
					nr_cl: '',
					nr_arfcn: '',
					ss_rsrp: '',
					ss_rlnr: '',
					download_5grate: '',
					upload_5gspeed: '',
					frequency_4Gband: '',
					ecl: '',
					earfcn: '',
					rsrp: '',
					slnr: '',
					download_4grate: '',
					upload_4gspeed: '',
					test_5gphotos: '',
					test_4gphotos: '',
					engin_coverage_device_id: '',
				},

				currentSbIndex: 0,
				//覆盖设备的数据
				coverageofdevice_testingdata: [],
				current_coverageofdevice_testingdata: {
					number_code: '',
					coveragedevice_5gss_rsrp: '',
					coveragedevice_5gss_rinr: '',
					coveragedevice_5gdownloadrate: '',
					coveragedevice_5guploadspeed: '',
					coveragedevice_4grsrp: '',
					coveragedevice_4gsinr: '',
					coveragedevice_4gdownloadrate: '',
					coveragedevice_4guploadspeed: '',
					coveragedevice_5gimg: '',
					coveragedevice_4gimg: '',
				},

				fileList1: [],
				fileList2: [],
				fileList3: [],
				fileList4: [],
				fgsb: []
			}
		},
		components: {},
		filters: {},
		mounted() {},
		onLoad(e) {
			this.id = e.id
			// this.getFgsb()
			this.lookInfo()
		},
		watch:{
			coverageofdevice_testingdata: {
			    handler(newVal, oldVal) { 
			      this.checkTestData()
			    },
			    deep: true
			} 
		},
		onShow() {},
		onPageScroll(e) {},
		beforeDestroy() {},
		methods: {
			//检查是否存在输入数据
			checkTestData(){
				let rst = this.coverageofdevice_testingdata
				let  newArray = rst.map(({ id,type,association_id,number_code,is_full, ...rest }) => rest); 
				newArray.forEach((item, index)=>{
					
					item.coveragedevice_5gimg = item.coveragedevice_5gimg?item.coveragedevice_5gimg.join(','):''
					item.coveragedevice_4gimg = item.coveragedevice_4gimg?item.coveragedevice_4gimg.join(','):''
					
					let temp_val = Object.values(item);   
					const hasEmptyValue = temp_val.some(item => !item);
					if (hasEmptyValue) {
						this.coverageofdevice_testingdata[index]['is_full'] = false; 
					} else {
						this.coverageofdevice_testingdata[index]['is_full'] = true; 
					}
					
				}) 
			},
			chooseSb(item, index) {
				this.current_coverageofdevice_testingdata = item
				this.currentSbIndex = index

				let temp_3 = []
				item.coveragedevice_5gimg = item.coveragedevice_5gimg ? item.coveragedevice_5gimg : []
				item.coveragedevice_5gimg.forEach((item, index) => {
					if (item.url) {
						temp_3.push({
							url: item.url
						})
					} else {
						temp_3.push({
							url: item
						})
					}

				})
				this.fileList3 = temp_3
				let temp_4 = []
				item.coveragedevice_4gimg = item.coveragedevice_4gimg ? item.coveragedevice_4gimg : []
				item.coveragedevice_4gimg.forEach((item, index) => {
					if (item.url) {
						temp_4.push({
							url: item.url
						})
					} else {
						temp_4.push({
							url: item
						})
					}
				})
				this.fileList4 = temp_4
			},
			// isInArray(item, arr) {
			// 	return arr.includes(item);
			// },
			//网络设备
			// getFgsb() {
			// 	uni.showLoading({})
			// 	api.post('/api/shop/engin_coverage_device', {}).then(res => {
			// 		let rst = res.data
			// 		uni.hideLoading()
			// 		if (rst.code == 1) {
			// 			this.fgsb = rst.data
			// 		}
			// 	}).catch(err => {
			// 		uni.hideLoading()
			// 	})
			// },
			lookInfo() {
				uni.showLoading({})
				api.post('api/shop/submit_networktesting', {
					engin_id: this.id,
					type: 2
				}).then(res => {
					let rst = res.data
					uni.hideLoading()
					if (rst.code == 1) {
						if (rst.data) {
							this.params = rst.data
							if (rst.data.coverageofdevice_testingdata.length > 0) {
								this.chooseSb(rst.data.coverageofdevice_testingdata[0], 0)
							}
							this.coverageofdevice_testingdata = rst.data.coverageofdevice_testingdata
							 
						 
							let temp_1 = []
							rst.data.test_5gphotos = rst.data.test_5gphotos ? rst.data.test_5gphotos : []
							rst.data.test_5gphotos.forEach((item, index) => {
								temp_1.push({
									url: item
								})
							})
							this.fileList1 = temp_1

							let temp_2 = []
							rst.data.test_4gphotos = rst.data.test_4gphotos ? rst.data.test_4gphotos : []
							rst.data.test_4gphotos.forEach((item, index) => {
								temp_2.push({
									url: item
								})
							})
							this.fileList2 = temp_2
							
							this.checkTestData();
						}
						
					} 
				}).catch(err => {
					uni.hideLoading()
				})
			},

			next() {
				let rst = this.params
				rst.test_5gphotos = this.fileList1 ? this.fileList1.map(item => item.url).join(',') : [];
				rst.test_4gphotos = this.fileList2 ? this.fileList2.map(item => item.url).join(',') : [];
				
				let issb = true;
				
				let rst_test_img = this.coverageofdevice_testingdata ? this.coverageofdevice_testingdata : [] 
				rst_test_img.forEach(function(item){
					if (!item.is_full) {
						 issb = false
					}
				})
				if (!issb) {
					uni.$u.toast("请完善覆盖设备信息")
					return
				}
				rst_test_img = rst_test_img.map(({ is_full,id, ...rest }) => rest)
 				rst_test_img.forEach(function(itemrst, index) {
					rst_test_img[index].coveragedevice_5gimg = itemrst.coveragedevice_5gimg ? itemrst
						.coveragedevice_5gimg.map(function(item){
							if (item.url){
								return item.url
							} else {
								return item
							}
						}).join(',') : '';
					rst_test_img[index].coveragedevice_4gimg = itemrst.coveragedevice_4gimg ? itemrst
						.coveragedevice_4gimg.map(function(item){
							if (item.url){
								return item.url
							} else {
								return item
							}
						}).join(',') : '';
				});
				rst.coverageofdevice_testingdata = rst_test_img
				rst.engin_coverage_device_id = this.currentSbids.join(',')

				uni.showLoading({})
				api.post('api/shop/submit_networktesting', {
					engin_id: this.id,
					...rst,
					type: 1
				}).then(res => {
					let rst = res.data
					uni.hideLoading()
					
					if (rst.code == 1) {
						uni.navigateTo({
							url: '/pages/gcaz/jgys?id=' + this.id
						})
					} else {
						uni.$u.toast(rst.msg)
					}

				}).catch(err => {
					uni.hideLoading()
				})

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				if (event.name == 3) {
					this.coverageofdevice_testingdata[this.currentSbIndex].coveragedevice_5gimg = this[
						`fileList${event.name}`] 
				}
				if (event.name == 4) {
					this.coverageofdevice_testingdata[this.currentSbIndex].coveragedevice_4gimg = this[
						`fileList${event.name}`] 
				}
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					if (event.name == 3) {
						this.coverageofdevice_testingdata[this.currentSbIndex].coveragedevice_5gimg = this[
							`fileList${event.name}`] 
					}
					if (event.name == 4) {
						this.coverageofdevice_testingdata[this.currentSbIndex].coveragedevice_4gimg = this[
							`fileList${event.name}`] 
					}
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: apiBaseUrl + '/api/shop/upload',
						filePath: url,
						header: {
							'method': "xj_user",
							'token': uni.getStorageSync('token')
						},
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								let rst = JSON.parse(res.data)
								if (rst.code == 1) {
									resolve(rst.data)
								} else {
									uni.$u.toast(rst.msg)
									this.deletePic(event)
								}
							}, 1000)
						}
					});
				})
			},
		}
	};
</script>

<style scoped>
	.info_box_base_btns_item_active {
		background-color: rgb(242, 64, 64) !important;
	}

	.info_box_base_btns_item {
		font-size: 25rpx;
		text-align: center;
		color: rgb(51, 51, 51);
		white-space: pre-wrap;
		word-break: break-word;
		max-width: 148rpx;
		background-color: rgb(17, 200, 97);
		padding: 10rpx 20rpx;
		border-radius: 10rpx;
		margin-right: 40rpx;
		margin-top: 20rpx;
	}

	.info_box_base_btns {
		display: flex;
		flex-direction: row;
		align-items: center;
		flex-wrap: wrap;
	}

	.btn {
		width: 500rpx;
		height: 80rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		margin: 0 auto;
		margin-top: 40rpx;
		background-color: rgb(70, 179, 255);
		color: #fff;
		border-radius: 40rpx;
		font-size: 32rpx;
	}

	::v-deep .uni-input-input {
		font-size: 26rpx;
	}

	::v-deep .uni-input-placeholder {
		font-size: 26rpx;
	}

	.info_box_base_item_img_title {
		font-size: 25rpx;
		margin: 20rpx 0;
	}

	.gcxt_b_wy_reason {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.gcxt_b_wy {
		display: flex;
		flex-direction: row;
		align-items: center;
		margin: 20rpx 0;
	}

	.info_box_base {
		border: 1px solid rgb(235, 229, 229);
		padding: 20rpx;
		font-size: 25rpx;
		margin-top: 20rpx;
	}

	.info_box_base_item {
		line-height: 45rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.info_box {
		width: 710rpx;
		margin: 0 auto;

	}

	page {
		background-color: #fff;
	}
</style>